﻿using Furion.DependencyInjection;
using OnWork.DataCenter.BLL.Base.Dto;
using OnWork.DataCenter.DAL;
using OnWork.DataCenter.DAL.DataCenter;
using OnWork.DataCenter.Utils.Caches;
using SqlSugar;

namespace OnWork.DataCenter.BLL.DataCenter
{
    /// <summary>
    /// 数据连接业务
    /// </summary>
    public class DBConnBLL : IScoped
    {
        /// <summary>
        /// 数据库地址
        /// </summary>
        private SqlSugarScope db = DBManger.DataCenter;

        public DBConnBLL()
        {
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<SQLReturn> AddDAsync(DBConn input)
        {
            input.name = input.name.Trim();
            bool isExit = await db.Queryable<DBConn>().Where(x => x.name == input.name).AnyAsync();
            if (isExit)
            {
                return SQLReturn.AddError($"错误:数据连接名称:{input.name}已存在!");
            }
            var rows = await db.Insertable(input).ExecuteReturnSnowflakeIdAsync();
            return SQLReturn.AddSuccess();
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<SQLReturn> DeleteAsync(long[] ids)
        {
            if (ids == null || ids.Length == 0)
            {
                return SQLReturn.DelError($"错误:删除Id不存在!");
            }
            var rows = await db.Deleteable<DBConn>().In(ids).ExecuteCommandAsync();
            SqlSugarCache.RemoveAll();//移除缓存
            return SQLReturn.DelSuccess(rows);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<SQLReturn> UpdateDatabaseAsync(DBConn input)
        {
            var old = await db.Queryable<DBConn>().InSingleAsync(input.id);

            input.name = input.name.Trim();

            if (!string.IsNullOrEmpty(input.name))
            {
                if (db.Queryable<DBConn>().Where(x => x.name == input.name && x.name != old.name).Any())
                {
                    return SQLReturn.UpdateError($"错误:数据连接名称:{input.name}已存在!");
                }
            }
            var rows = await db.Updateable(input).ExecuteCommandAsync();
            SqlSugarCache.RemoveAll();//移除缓存
            return SQLReturn.UpdateSuccess(rows);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="option"></param>
        /// <returns></returns>
        public async Task<SQLReturn<List<DBConn>>> QueryPagedListAsync(BaseQueryPageInput option)
        {
            var querysql = option.ToQuerySQL<DBConn>();

            RefAsync<int> total = 0;
            var query = await db.Queryable<DBConn>()
                .WhereIF(!string.IsNullOrEmpty(querysql.Where), querysql.Where)
                .OrderByIF(!string.IsNullOrEmpty(querysql.Order), $"{querysql.Order}")
                .ToPageListAsync(option.PageIndex, option.PageItems, total);
            return SQLReturn<DBConn>.QueryListSuccess(query, total.Value);
        }

        /// <summary>
        /// 获取全部可用的数据连接
        /// </summary>
        /// <returns></returns>
        public async Task<List<DBConn>> GetALLAsync()
        {
            return await db.Queryable<DBConn>().ToListAsync();
        }

        /// <summary>
        /// 获取全部可用的数据连接
        /// </summary>
        /// <returns></returns>
        public List<DBConn> GetALL()
        {
            return db.Queryable<DBConn>().ToList();
        }
    }
}